$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/users/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '用户名', name: 'username', index: 'username', width: 80 },
            { label: '头像', name: 'headimg', index: 'headimg', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.headimg!=null&&rowdata.headimg!=''){
                        return '<img src='+rowdata.headimg+' width="50" height="50"/>';
                    }else{
                        return '';
                    }

                } },
            { label: 'openid', name: 'openid', index: 'openid', width: 80 },
			{ label: '手机号', name: 'telphone', index: 'telphone', width: 80 },
            { label: '会员等级', name: 'level', index: 'level', width: 80,formatter: function(item, index) {
                    if (item === 0) {
                        return '<span class="label label-success">普通会员</span>';
                    }
                    if (item === 1) {
                        return '<span class="label label-danger">VIP会员</span>';
                    }
                } },
            { label: '创建时间', name: 'createtime', index: 'createtime', width: 80 },
			{ label: '状态', name: 'statu', index: 'statu', width: 80,formatter: function(item, index) {
                    if (item === 0) {
                        return '启用';
                    }
                    if (item === 1) {
                        return '禁用';
                    }
                } },

			{ label: '操作', name: 'sy',width: 80,formatter: function(cellvalue, options, rowdata) {
                    return "<a style='cursor:pointer;' onclick='vm.getInfo("+ rowdata.id +")'>" +
                        "详情</a>";
                } }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			username:null
		},
		showList: true,
		title: null,
		users: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.users = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.users.id == null ? "sys/users/save" : "sys/users/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.users),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/users/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/users/info/"+id,
                success: function(r){
                    $("#openid").text(r.users.openid);
                    $("#tel").text(r.users.telphone);
                    $("#statu").text(r.users.statu==0?"启用":"禁用");
                    $("#createtime").text(r.users.createtime);
                    $("#lastloginip").text(r.users.lastloginip);
                    $("#lastlogintime").text(r.users.lastlogintime);
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{
                    username:vm.q.username
                },
            }).trigger("reloadGrid");
		}
	}
});